package com.jike.noobmoney.mvp.view.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jike.noobmoney.R;
import com.jike.noobmoney.util.ToastUtils;


/**
 * Created by kekex on 2018/9/11.
 * 退出登录弹框(中间弹出)
 */

public class TopTaskDialog extends DialogFragment implements View.OnClickListener {

    private Context context;
    private Dialog dialog;
    private View view;
    EditText et_text;
    TextView tv_show;
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private String mParam1;
    private double mParam2;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }



    public static TopTaskDialog newInstance(String param1,double topmoney) {
        TopTaskDialog fragment = new TopTaskDialog();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putDouble(ARG_PARAM2, topmoney);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getDouble(ARG_PARAM2);
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        dialog = new Dialog(context, R.style.BindPhonedialog);
        view = LayoutInflater.from(context).inflate(R.layout.dialog_top_task, null);
        et_text= (EditText) view.findViewById(R.id.et_text);
        tv_show = (TextView) view.findViewById(R.id.tv_show);
        tv_show.setText(""+mParam2);
        TextView tv_confirm = (TextView) view.findViewById(R.id.tv_confirm);

        TextView tv_show_id = (TextView) view.findViewById(R.id.tv_show_id);
        tv_show_id.setText("注：置顶功能收费标准为"+mParam2+"元/小时");
        ImageView img_close=(ImageView)view.findViewById(R.id.img_close);


        tv_confirm.setOnClickListener(this);
        img_close.setOnClickListener(this);

        et_text.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(s.length()!=0) {
                    double money = Integer.parseInt(s.toString()) * mParam2;
                    tv_show.setText(money + "");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        //设置取消标题
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置点击外部不可以消失
        dialog.setCanceledOnTouchOutside(true);
        //设置即使点击返回键也不会退出
        setCancelable(true);
        dialog.setContentView(view);

        //获取当前Activity所在的窗体
        Window dialogWindow = dialog.getWindow();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.gravity = Gravity.CENTER;
        //设置Dialog宽度匹配屏幕宽度
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        //设置Dialog高度自适应
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //将属性设置给窗体
        dialogWindow.setAttributes(lp);
        return dialog;
    }

    public OnClickCallback clickCallback;

    public interface OnClickCallback {
        void confirm(String t_id,String money);
    }

    public void setClickCallback(OnClickCallback clickCallback) {
        this.clickCallback = clickCallback;
    }


    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.img_close) {
           this.dismiss();
        } else if (id == R.id.tv_confirm) {
            //确认
            String str=et_text.getText().toString();
            if(str==null||str.length()==0){
                Toast.makeText(getActivity(),"请输入置顶时间",Toast.LENGTH_SHORT).show();
                return;
            }else{
                if (null != clickCallback) {
                    clickCallback.confirm(mParam1,str);
                }
            }

        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }
    }


}
